package com.itheima.service.impl;


import com.itheima.common.CustomException;
import com.itheima.constant.CommonConstant;
import com.itheima.dto.ChangeCategoryDTO;
import com.itheima.dto.InsertCategoryDTO;
import com.itheima.entity.Category;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 *分类模块业务层实现类
 * @author msi
 * @date 2022/5/17
 * @version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper mapper;

    @Override
    public boolean insertCategory(InsertCategoryDTO insertCategoryDTO) {

        Category category = new Category();
        if (insertCategoryDTO == null) {
            return false;
        }
        BeanUtils.copyProperties(insertCategoryDTO, category);

        int i = mapper.insert(category);
        if (i <= 0) {
            throw new CustomException(category.getName());
        }
        return true;
    }

    /**
     * 修改
     *
     * @param categoryDTO 修改的分类对象
     * @return 返回布尔值
     */
    @Override
    public Boolean update(ChangeCategoryDTO categoryDTO) {
        Category category = new Category();
        if (categoryDTO == null) {
            return false;
        }
        BeanUtils.copyProperties(categoryDTO, category);
        //找到要被修改的对象将创建用户复制过来。
        Category cForCreate = mapper.selectById(category.getId());
        category.setCreateUser(cForCreate.getCreateUser());

        int i = mapper.updateById(category);
        if (i <= 0) {
            throw new CustomException("信息修改失败");
        }
        return true;
    }

    /**
     * 删除
     *
     * @param id 分类id
     * @return 返回成功与否
     */
    @Override
    public Boolean deleteById(String id) {

        Category category = mapper.selectById(id);
        if (category.getType() == CommonConstant.CANNOT_DELETE) {
            throw new CustomException("套餐不能删哦");
        }
        int i = mapper.deleteById(id);
        if (i <= CommonConstant.DELETE_FAILED) {
            throw new CustomException("删除失败");
        }

        return true;
    }

    @Override
    public List<Category> selectList(Integer type) {

        if (type==null){
            return mapper.selectList();
        }
        return mapper.selectListByType(type);
    }

    @Override
    public ResultVO selectPage(Long page, Long pageSize) {
        ResultVO resultVO = new ResultVO();
        if (page == null || pageSize == null) {
            throw new CustomException("页码为空");
        }

        long index = (page-1)*pageSize;
        List<Category> records = mapper.selectPages(index, pageSize);
        long total = mapper.getTotal();
        long pages = total/pageSize;
        resultVO.setRecords(records);
        resultVO.setPages(pages);
        resultVO.setTotal(total);
        resultVO.setCurrent(page);
        resultVO.setSize(pageSize);

        return resultVO;
    }
}
